import { QCcode } from "./request.js";
import "./html2canvas.js"
let iname = document.querySelector(".iname")
let time = document.querySelector('.time')
let date = document.querySelector('.date')

let back = document.querySelector('#back')
let cont = document.querySelector('#continue')
let apc = document.querySelector('#apc')
let saveBtn = document.querySelector('.save')
let codeImg = document.querySelector('#code_img')
async function getCode() {
    // let url = 'http://newshopapi.0melon0.cn/api/f_user/Qrcode'
    let data = {
        token: sessionStorage.getItem("token"),
        id: sessionStorage.getItem("id"),
    }
    console.log(data);
    let res = await QCcode(data)

    iname.innerHTML = sessionStorage.getItem('name')
    date.innerHTML = sessionStorage.getItem('day_time')
    time.innerHTML = sessionStorage.getItem('show_name')
    codeImg.src = res.data.data
    console.log(res);

    back.addEventListener('click', function () {
        location.href = "../index.html"
    })
    cont.addEventListener('click', function () {
        location.href = "../sessions.html"
    })
    apc.addEventListener('click', function () {
        location.href = "../my.html"
    })
    //截图canvas
    saveBtn.addEventListener('click', function () {
        screenShot();
    })
    // 生成截图过程
    function screenShot() {
        html2canvas(document.body, {
            allowTaint: true,
            useCORS: true
        }).then(canvas => {
            let a = document.createElement('a');
            let img = new Image();
            img.src = canvas.toDataURL();
            a.appendChild(img);
            a.href = img.src;
            a.download = "保存预约二维码";
            document.body.appendChild(a);
            setTimeout(function () {
                img.style.transform = "scale(0.6)";
            }, 0)
            setTimeout(function () {
                a.click();
                a.remove();
            }, 1000)
        });
    }

}
getCode()